package com.learn.controller;

import com.learn.pojo.Dept;
import com.learn.pojo.Result;
import com.learn.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    /*
    * 查询所有部门
     */
//    @RequestMapping(value = "list",method = RequestMethod.GET)
//    private Logger logger = Logger.getLogger(DeptController.class);
    @Autowired
    DeptService deptService;
    @GetMapping
    public Result list(){
        log.info("查询所有部门");
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    /*
    *根据id删除部门
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除的部门id为{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    /*
    * 新增部门
     */
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门{}",dept.getName());
        deptService.add(dept);
        return Result.success();
    }

    /*
    * 根据id查询信息
     */
    @GetMapping("/{id}")
    public Result getDataById(@PathVariable Integer id){
        log.info("查询的部门id为{}",id);
        Dept dept = deptService.getDataById(id);
        return Result.success(dept);
    }

    /*
    * 修改部门
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门{}",dept);
        deptService.update(dept);
        return Result.success();
    }

}
